﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Accountant_CompanyDetail : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    void Bind()
    {
        string company = Request.QueryString["CompanyID"];
        string orderid = Request.QueryString["orderid"];

        GridView1.DataSource = new AuditorsBLL().GetCompanyDetail(company, orderid);
        GridView1.DataBind();

        double expectProfitTax = new AuditorsBLL().GetProfitTax(company, orderid);
        double actualProfit = new AuditorsBLL().GetProfit(company, orderid);
        double actualProfitTax = actualProfit * 0.165;

        StringBuilder sb = new StringBuilder();
        sb.Append("<table>");
        sb.Append("<tr>");
        sb.AppendFormat("<td>{0}</td><td>{1}</td>", "期望利得税", expectProfitTax);
        sb.AppendFormat("<td>{0}</td><td>{1}</td>", "实际利得税", actualProfitTax);
        sb.AppendFormat("<td>{0}</td><td>还应补{1}开支(负数应补收入，或减少开支)</td>", "差额", (actualProfitTax - expectProfitTax) / 0.165);
        sb.Append("</tr>");
        sb.Append("</table>");

        //Label1.Text = sb.ToString();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    if (list == null)
        //    {
        //        Bind();
        //    }

        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        string str = e.Row.Cells[1].Text;

        //        e.Row.Cells[1].Text = list.First(fun => fun.TypeId == str).TypeName;
        //    }
        //}
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string company = Request.QueryString["CompanyID"];
        string orderid = Request.QueryString["orderid"];
        List<CompanyDetailEntity> list = new List<CompanyDetailEntity>();
        foreach (GridViewRow row in GridView1.Rows)
        {
            try
            {
                list.Add(new CompanyDetailEntity()
                {
                    Amount = Convert.ToDouble((row.Cells[2].FindControl("TextBox1") as TextBox).Text),
                    CompanyId = company,
                    Date = "2000-1-1",
                    DetailTypesId = (row.Cells[2].FindControl("TextBox2") as TextBox).Text,
                    OrderId = orderid

                });
            }
            catch (Exception)
            {
                AlertMsg(string.Format("{0}金额不正确", row.Cells[0].Text));
                return;
            }
        }
        string msg;

        if (new AuditorsBLL().CreateCompanyDetail(company, orderid, list, out msg))
        {
        }
        else
        {
            if (msg.ToLower().StartsWith("sql自定义异常:"))
            {
                AlertMsg(msg.ToLower().Replace("sql自定义异常:", ""));
            }
            else
            {
                AlertMsg("插入失败，请检查数据是否异常");
            }
        }

        Bind();
    }
}